import {
    url
} from '../../modelApi/url_config.js'
const App = getApp()
let statusBarHeight = getApp().globalData.navHeight;
Page({
    data: {
        statusBarHeight: statusBarHeight,
        shopcartlist: [], // 购物车列表
        shopcartLength: 0, // 购物车的长度
        totalPrice: 0, // 总价，初始为0
        selectAllStatus: true, // 全选状态，默认全选
        editorCartFlag: true,
        changeShopItemid: '',
        jiesuanDisable: false,
        isShow: false,
    },
    onShow() {
    },
    onLoad() {
        this.geCartListFun() // 获取购物车列表
    },
    addShopCarFun: function (type) {
        let params = {
            itemId: this.data.changeShopItemid,
            number: type
            // skuId: this.data.goodsId
        }
        App.ajax(url.cartUpdateUrl, params, 'post').then(res => {})
    },
    /* 获取购物车列表 */
    geCartListFun() {
        return App.ajax(url.cartList, {}, 'get').then(res => {
            let shopLength = "";
            const data = res.data.map(el => {
                shopLength += el.cartGoods.length;
                el.cartGoods = el.cartGoods.map(childel => {
                    return {
                        ...childel,
                        ...{ selected: true }
                    }
                })
                return {
                    ...el,
                    ...{ selected: true }
                }
            })
            this.setData({
                shopcartlist: data,
                shopcartLength: Number(shopLength),
                isShow: true
            })
            this.getTotalPrice();
        })
    },
    /* 清空购物车 */
    cartEmptyFun() {
        App.ajax(url.cartEmpty, {}, 'POST').then(res => {})
    },
    notgoto() {
        const shopcartlist = this.data.shopcartlist
        const checklist = [];
        shopcartlist.forEach(el => {
            el.cartGoods.forEach(childel => {
                if (childel.selected) {
                    checklist.push(el)
                }
            })
            
        })
        this.setData({
            selectAllStatus: checklist.length === this.data.shopcartLength ? true : false
        })
        this.setData({
            jiesuanDisable: !checklist.length ? true : false,
        })
    },
	// 当前商品选中事件
    selectList(e) {
        const index = e.currentTarget.dataset.index;
        const parentsindex = e.currentTarget.dataset.parentsindex !=-1 ? e.currentTarget.dataset.parentsindex : null;
        let shopcartlist = this.data.shopcartlist;
        if (parentsindex !=undefined && parentsindex != -1) {
            const selected = shopcartlist[parentsindex].cartGoods[index].selected;
            shopcartlist[parentsindex].cartGoods[index].selected = !selected;
            let selectLength = 0;
            shopcartlist[parentsindex].cartGoods.map(childel => {
                childel.selected && selectLength++
            })
            selectLength === shopcartlist[parentsindex].cartGoods.length ? shopcartlist[parentsindex].selected = true : shopcartlist[parentsindex].selected = false;
            //  shopcartlist[parentsindex].selected = !shopcartlist[parentsindex].selected
        } else {
           const selected = shopcartlist[index].selected;
            shopcartlist[index].selected = !selected;
            shopcartlist[index].cartGoods.map(childel => {
                selected ? childel.selected = false : childel.selected = true;
            })
        }
        
        this.notgoto()
        this.setData({
            shopcartlist: shopcartlist
        });
        if (this.data.editorCartFlag) {
            this.getTotalPrice();
        }
    },
    deleteList() {
        if (this.data.jiesuanDisable) return
        let params = {
            itemIds: ""
        }; //删除的商品id
        let paramsIndex = []; //删除商品的下标
        this.data.shopcartlist.forEach((element, index) => {
            element.cartGoods.forEach((childEl, childIndex) => {
                if (childEl.selected) {
                    params.itemIds = params.itemIds != "" ? params.itemIds + "," + childEl.itemId : childEl.itemId;
                    paramsIndex.push(childEl.goodsId);
                }
            })
        });
        if (params.itemIds == "") {
            return;
        }
        App.ajax(url.cartRemove, params, 'POST').then(res => {
            let shopcartlist = this.data.shopcartlist;
            paramsIndex.forEach((element) => {
                shopcartlist.forEach((ele, ind) => {
                    ele.cartGoods.forEach((childele, childInd) => {
                        if (element == childele.goodsId) {
                            ele.cartGoods.splice(childInd, 1);
                        }
                    })
                })
            })
            this.setData({
                shopcartlist: shopcartlist
            });
            if (shopcartlist.length) {
                this.getTotalPrice();
            }
        })
    },
    selectAll(e) {
        let selectAllStatus = this.data.selectAllStatus;
        selectAllStatus = !selectAllStatus;
        let shopcartlist = this.data.shopcartlist;
        for (let i = 0; i < shopcartlist.length; i++) {
            shopcartlist[i].selected = selectAllStatus;
            shopcartlist[i].cartGoods.map(el => {
                el.selected = selectAllStatus
            })
        }
        this.setData({
            selectAllStatus: selectAllStatus,
            shopcartlist: shopcartlist
        });
        this.notgoto()
        if (this.data.editorCartFlag) {
            this.getTotalPrice();
        }
    },
	// 绑定加数量事件
    addCount(e) {
        const index = e.currentTarget.dataset.index;
        const parentsindex = e.currentTarget.dataset.parentsindex;
        let shopcartlist = this.data.shopcartlist;
        let quantity = shopcartlist[parentsindex].cartGoods[index].quantity;
        quantity = quantity + 1;
        shopcartlist[parentsindex].cartGoods[index].quantity = quantity;
        this.setData({
            shopcartlist: shopcartlist,
            changeShopItemid: e.currentTarget.dataset.itemid
        });
        this.getTotalPrice();
        this.addShopCarFun(1)
    },
	// 绑定减数量事件
    minusCount(e) {
        const index = e.currentTarget.dataset.index;
        const parentsindex = e.currentTarget.dataset.parentsindex;
        let shopcartlist = this.data.shopcartlist;
        let quantity = shopcartlist[parentsindex].cartGoods[index].quantity;
        if (quantity <= 1) {
            return false;
        }
        quantity = quantity - 1;
        shopcartlist[parentsindex].cartGoods[index].quantity = quantity;
        this.setData({
            shopcartlist: shopcartlist,
            changeShopItemid: e.currentTarget.dataset.itemid
        });
        this.getTotalPrice();
        this.addShopCarFun(-1)
    },
	// 计算总价
    getTotalPrice() {
        let shopcartlist = this.data.shopcartlist; // 获取购物车列表
        let total = 0;
        for (let i = 0; i < shopcartlist.length; i++) { // 循环列表得到每个数据
            shopcartlist[i].cartGoods.forEach(el => {
                if (el.selected) { // 判断选中才会计算价格
                    total += el.quantity * el.price; // 所有价格加起来
                }
            })
        }
        this.setData({ // 最后赋值到data中渲染到页面
            shopcartlist: shopcartlist,
            totalPrice: total.toFixed(2)
        });
    },
    /* 结算按钮 */
    jiesuanFunc() {
        if (this.data.jiesuanDisable) return
        let params = {
            orderByCart: 1,
            goodsIds: "",
            itemIds: ""
        }
        this.data.shopcartlist.forEach(element => {
            element.cartGoods.forEach((childel, childInd) => {
                if (childel.selected) {
                    params.goodsIds = params.goodsIds != "" ? params.goodsIds + "," + childel.goodsId : childel.goodsId;
                    params.itemIds = params.itemIds != "" ? params.itemIds + "," + childel.itemId : childel.itemId;
                }
            })
        });
        App.ajax(url.orderGenerateUrl, params, 'POST').then(res => {
            if (res.code == 0) {
                wx.setStorageSync('orderSettlementData', res.data)
                wx.navigateTo({
                    url: '/pages/mine/sonpages/order/orderSettlement/index',
                })
            }
        })
    },
    editorCart() {
        let shopcartlist = this.data.shopcartlist;
        for (let i = 0; i < shopcartlist.length; i++) {
            shopcartlist[i].selected = false;
            shopcartlist[i].cartGoods.map(el => {
                el.selected = false;
            })
        }
        this.setData({
            selectAllStatus: false,
            editorCartFlag: false,
            jiesuanDisable: true,
            shopcartlist: shopcartlist
        })
    },
    editorCartFinsh() {
        let shopcartlist = this.data.shopcartlist;
        for (let i = 0; i < shopcartlist.length; i++) {
            shopcartlist[i].selected = true;
            shopcartlist[i].cartGoods.map(el => {
                el.selected = true;
            })
        }
        this.setData({
            selectAllStatus: true,
            editorCartFlag: true,
            jiesuanDisable: false,
            shopcartlist: shopcartlist
        })
        this.getTotalPrice();
    },
    toHome() {
        wx.reLaunch({
            url: '/pages/index/index'
        })
    }

})